package hauwei.year2025.month4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class MinWay {
    public static List<String> re=new ArrayList<>();
    public static int minSum=Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int stop=Integer.parseInt(sc.nextLine());
        String se=sc.nextLine();
        String[] sae = se.split(" ");
        List<String []> map=new ArrayList<>();
        for(int i=0;i<stop;i++){
            String line=sc.nextLine();
            if(line.equals("0000")){
                break;
            }
            String[] sline = line.split(" ");
            map.add(sline);
        }
        sc.close();
        int []visited=new int[map.size()];
        for(int i=0;i<map.size();i++){
            if(map.get(i)[0].equals(sae[0])){
                dfs(sae[1],0,new ArrayList<>(),i,map,visited,0);
            }
            else if(map.get(i)[1].equals(sae[0])){
                dfs(sae[1],0,new ArrayList<>(),i,map,visited,1);
            }
        }
        for(int i=0;i<re.size();i++){
            System.out.print(re.get(i));
            if(i<re.size()-1){
                System.out.print(" ");
            }
        }
    }
    public static void dfs(String end,int sum,List<String> list,int index,List<String []> map,int[]visted,int onetwo){
        if(visted[index]==1){
            return;
        }
        visted[index]=1;
        int nowi=onetwo;
        int nexti=1-onetwo;
        int len = Integer.parseInt(map.get(index)[2]);
        String now=map.get(index)[nowi];
        String next=map.get(index)[nexti];
        list.add(now);
        if(next.equals(end)){
            int nowSum=sum+len;
            if(minSum>nowSum){
                list.add(next);
                minSum=nowSum;
                re.clear();
                re.addAll(list);
            }
        }
        for(int k=0;k<map.size();k++){
            if(visted[k]==0){
                String[] nexts = map.get(k);
                if(nexts[0].equals(next)){
                    dfs(end,sum+len,new ArrayList<>(list),k,map,visted,0);
                } else if(nexts[1].equals(next)){
                    dfs(end,sum+len,new ArrayList<>(list),k,map,visted,1);
                }
            }
        }
        visted[index]=0;
    }
}
